Internet Analytics - Lab 2
Group: B
Names:
This is a template for part 3 of the lab. Clearly write your answers, comments and interpretations in Markodown cells. Don't forget that you can add $\LaTeX$ equations in these cells. Feel free to add or remove any cell.
Please properly comment your code. Code readability will be considered for grading. To avoid long cells of codes in the notebook, you can also embed long python functions and classes in a separate module. Don’t forget to hand in your module if that is the case. In multiple exercises, you are required to come up with your own method to solve various problems. Be creative and clearly motivate and explain your methods. Creativity and clarity will be considered for grading.
# ... WRITE YOUR CODE HERE...
import json
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import networkx as nx
import epidemics_helper
nyc_augmented_network_json = open("../data/nyc_augmented_network.json", "r")
data = json.load(nyc_augmented_network_json)
data_nodes = []
data_pos = {}
data_edges = []
# make a list of nodes and a dictionary of positions
for dic in data['nodes']:
data_nodes.append(dic['id'])
data_pos[dic['id']] = dic['coordinates']
# make edges list
for dic in data['links']:
data_edges.append((dic['source'], dic['target']))
G1 = nx.Graph()
G1.add_nodes_from(data_nodes)
G1.add_edges_from(data_edges)
# Simulate the Plague epidemic
sir = epidemics_helper.SimulationSIR(G1, beta=10.0, gamma=0.1)
sir.launch_epidemic(source=23654, max_time=100.0)
# make a ï¼” rows 100 columns matrix.
# Rows correspond to numbers of day, S nodes, I nodes and R nodes.
numbers_SIR = np.zeros((4, 100), dtype = 'int')
numbers_SIR[0] = np.arange(100)
for day in numbers_SIR[0]:
for node in data_nodes:
if sir.inf_time[node] > day:
numbers_SIR[1, day] += 1 # add 1 to numbers of S
elif sir.inf_time[node] <= day and day < sir.rec_time[node]:
numbers_SIR[2, day] += 1 # add 1 to numbers of I
else:
numbers_SIR[3, day] += 1 # add 1 to numbers of R
# plotting
plt.figure(figsize=(10, 8))
plt.bar(numbers_SIR[0], numbers_SIR[1], color = 'green', label = "S")
plt.bar(numbers_SIR[0], numbers_SIR[2], bottom = numbers_SIR[1], color = 'blue', label = "I")
plt.bar(numbers_SIR[0], numbers_SIR[3], bottom = numbers_SIR[1] + numbers_SIR[2], color = 'orange', label = "R")
plt.title("Evolution of the epidemic")
plt.xlabel("days")
plt.ylabel("nodes")
plt.legend()
is_dead = False
is_infected = False
is_infected_or_dead = False
for day in numbers_SIR[0]:
if (numbers_SIR[2][day]) / len(data_nodes) >= 0.6 and not is_infected:
print("It takes {} days before 60% of the population is infeceted.".format(day))
is_infected = True
if (numbers_SIR[3][day]) / len(data_nodes) >= 0.6 and not is_dead:
print("It takes {} days before 60% of the population is dead.".format(day))
is_dead = True
if (numbers_SIR[2][day] + numbers_SIR[3][day]) / len(data_nodes) >= 0.6 and not is_infected_or_dead:
print("It takes {} days before 60% of the population is infeceted or dead.".format(day))
is_infected_or_dead = True
def draw(day, graph, axisoff='off'):
color_map = []
for node in data_nodes:
if sir.inf_time[node] > day:
color_map.append('blue') # S nodes are blue
elif sir.inf_time[node] <= day and day < sir.rec_time[node]:
color_map.append('red') # I nodes are red
else:
color_map.append('green') # R nodes are green
plt.figure(figsize=(12, 12))
nx.draw(graph, pos=data_pos, node_color = color_map, with_labels = False, node_size = 1, width=0.5)
plt.axis(axisoff)
draw(1, G1)
draw(3, G1)
draw(30, G1)
import random
data_edges2 = random.sample(data_edges, len(data_edges)-1000)
G2 = nx.Graph()
G2.add_nodes_from(data_nodes)
G2.add_edges_from(data_edges2)
sir = epidemics_helper.SimulationSIR(G2, beta=10.0, gamma=0.1)
sir.launch_epidemic(source = random.sample(data_nodes, 1)[0], max_time=100.0)
draw(1, G2)
draw(3, G2)
draw(30, G2)
# make three lists correspond to S, I and R. Append them for repeat times.
S, I, R = [], [], []
repeat_time = 10
sir = epidemics_helper.SimulationSIR(G2, beta=10.0, gamma=0.1)
for i in range(repeat_time): # repeat for repeat_times
sir.launch_epidemic(source = random.sample(data_nodes, 1)[0], max_time=100.0)
s, i, r = 0, 0, 0 # s, i, r are the numbers of nodes at ith repeatment.
for node in data_nodes:
if sir.inf_time[node] > 30:
s += 1
elif sir.inf_time[node] <= 30 and 30 < sir.rec_time[node]:
i += 1
else:
r += 1
S.append(s)
I.append(i)
R.append(r)
print("average of healthy, infected, dead after 30 days= {}".format([sum(S) / len(S), sum(I) / len(I), sum(R) / len(I)]))
print("manage to keep {}% of healthy nodes in average after 30 days".format(sum(S) / len(S) / len(data_nodes)*100))
G3 = nx.Graph()
G3.add_nodes_from(data_nodes)
G3.add_edges_from(data_edges)
a = 0
for node in [node for node in G3.nodes if G3.degree(node) == 2]:
if a >= 2500:
break;
neighbors = list(G3.neighbors(node))
if len(neighbors) > 1:
G3.remove_edge(node, neighbors[0])
a += 1
S, I, R = [], [], []
repeat_time = 20
sir = epidemics_helper.SimulationSIR(G3, beta=10.0, gamma=0.1)
for i in range(repeat_time):
sir.launch_epidemic(source = random.sample(data_nodes, 1)[0], max_time=100.0)
s, i, r = 0, 0, 0
for node in data_nodes:
if sir.inf_time[node] > 30:
s += 1
elif sir.inf_time[node] <= 30 and 30 < sir.rec_time[node]:
i += 1
else:
r += 1
S.append(s)
I.append(i)
R.append(r)
print("average of healthy, infected, dead after 30 days= {}".format([sum(S) / len(S), sum(I) / len(I), sum(R) / len(I)]))
print("manage to keep {}% of healthy nodes in average after 30 days".format(sum(S) / len(S) / len(data_nodes)*100))
draw(30, G3)